/**
 * 
 */
package com.jie.eclipse.pan.swtitem;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.TreeItem;

/**
 * @author yanjie
 *
 */
public class ListGroup extends Composite{
	protected String xtype = "" ;//类型 radio or check or single
	protected Vector items = null ;
	
	/**
	 * @param parent
	 * @param style
	 */
	public ListGroup(Composite parent, int style) {
		super(parent, style);
	}
	
	
	//解决org.eclipse.swt.SWTException: Subclassing not allowed 
	@Override
	protected void checkSubclass() {
		// TODO Auto-generated method stub
	}
	
	public ListGroup(Composite parent,String xtype) {
		super(parent,SWT.NONE);
		this.xtype = xtype;
		this.items = new Vector();
	}
	
	public void setValues(Object values){
		if("radio".equals(this.getXtype())){
			for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
				Object obj = iterator.next();
				if (obj instanceof Button) {
					Button btn = (Button) obj;
					if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
						if(values.equals(btn.getData()) || String.valueOf(values).equals(String.valueOf(btn.getData())))
							btn.setSelection(true);
						else
							btn.setSelection(false);
					}
				}
			}
		}else if("check".equals(this.getXtype()) && values instanceof Object[]){
			for (int i = 0; i < ((Object[])values).length; i++) {
				Object value = ((Object[])values)[i];
				for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
					Object obj = iterator.next();
					if (obj instanceof Button) {
						Button btn = (Button) obj;
						if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
							if(value.equals(btn.getData()) || String.valueOf(value).equals(String.valueOf(btn.getData())))
								btn.setSelection(true);
							else
								btn.setSelection(false);
						}
					}
				}
			}
		}else if("singlecheck".equals(this.getXtype()) && values instanceof String){
			String value = (String)values;
			for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
				Object obj = iterator.next();
				if (obj instanceof Button) {
					Button btn = (Button) obj;
					if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
						if(value.equals(btn.getData()) || String.valueOf(value).equals(String.valueOf(btn.getData())))
							btn.setSelection(true);
						else
							btn.setSelection(false);
					}
				}
			}
		}
	}
	
	public void reset(){
		if("radio".equals(this.getXtype())){
			for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
				Object obj = iterator.next();
				if (obj instanceof Button) {
					Button btn = (Button) obj;
					if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
						btn.setSelection(false);
					}
				}
			}
		}else if("check".equals(this.getXtype())){
			for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
				Object obj = iterator.next();
				if (obj instanceof Button) {
					Button btn = (Button) obj;
					if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
						btn.setSelection(false);
					}
				}
			}
		}else if("singlecheck".equals(this.getXtype())){
			for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
				Object obj = iterator.next();
				if (obj instanceof Button) {
					Button btn = (Button) obj;
					if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
						btn.setSelection(false);
					}
				}
			}
		}
	}
	
	public Object getValues(){
		if(this.items == null)return null;
		ArrayList list = new ArrayList(); 
		for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
			Object obj = iterator.next();
			if (obj instanceof Button) {
				Button btn = (Button) obj;
				if((btn.getStyle()&SWT.RADIO) != 0 || (btn.getStyle()&SWT.CHECK) != 0){
					if(btn.getSelection()){
						list.add(btn.getData());
					}
				}
			}
		}
		
		if(list.size() > 0){
			if("radio".equals(this.getXtype())){
				return list.get(0);
			}else if("check".equals(this.getXtype())){
				Object values[] = new Object[list.size()];
				list.toArray(values);
				return values;
			}else if("singlecheck".equals(this.getXtype())){
				return list.get(0);
			}
			
		}
		return null;
	}
	
	/**
	 * @param style
	 * @return
	 */
	public Button getNewButton(){
		int style = SWT.NONE;
		if("check".equals(this.getXtype()) || "singlecheck".equals(this.getXtype())){
			style = SWT.CHECK;
		}else if("radio".equals(this.getXtype())){
			style = SWT.RADIO;
		}
		
		Button btn = new Button(this, style);
		this.addItem(btn);
		return btn;
	}
	
	public Button getNewButton(int style){
		Button btn = new Button(this, style);
		this.addItem(btn);
		return btn;
	}
	
	public String getXtype() {
		return xtype;
	}

	public void setXtype(String xtype) {
		this.xtype = xtype;
	}
	
	public void addItem(Object obj){
		this.items.add(obj);
	}
	
	public Vector getItems() {
		return items;
	}
	
}
